package
{
	import com.adobe.serialization.json.JSON;
	
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.external.ExternalInterface;
	import flash.net.LocalConnection;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.utils.Timer;
	
	import models.vo.UserVO;
	
	import org.dexter.DexterEvent;
	import org.dexter.sendDexterEvent;
	
	import vo.FlashVarsVO;
	
	public class WebServer extends URLLoader
	{
		[DexterBinding]
		public var stratus:Stratus;
		[DexterBinding]
		public var flashVars:FlashVarsVO;
		private var keepTimer:Timer;
		public var roomCode:String;
		private var login:URLVariables = new URLVariables();
		
		public function WebServer(interval:Number)
		{
			super(null);
			keepTimer = new Timer(interval);
			addEventListener(Event.COMPLETE,onData);
			keepTimer.addEventListener(TimerEvent.TIMER,onTimer);
		}
		private function onTimer(event:TimerEvent):void{
			load(new URLRequest(flashVars.keepPage+"?ops=keep&t="+Math.random()));
		}
		public function onData(event:Event):void{
			isLoading = false;
			var ops:Array = JSON.decode(data);
			if(ops.length)sendDexterEvent("callJs","jsTrace",data);
			for each(var op:Object in ops){
				switch(op.op){
					case "needPassword":
						sendDexterEvent("callJs","needPassword");
						break;
					case "init":
						UserVO.self.id = op.myId;
						sendDexterEvent("callJs","setSelfId",op.myId);
						sendDexterEvent("initUserList",op.userList);
						sendDexterEvent("initAppProperty",op.appProperty);
						sendDexterEvent("broadcast","userOnline",UserVO.self);
						sendDexterEvent("initPublishVideo");
						keepTimer.start();
						break;
					case "userOnline":
						sendDexterEvent("$userOnline",op.userData);
						break;
					case "userOffline":
						sendDexterEvent("$userOffline",op.id);
						break;
				}
			}
			if(requestList.length){
				load(requestList.shift());
			}
		}
		[DexterEvent]
		public function stratusConnected():void{
			login.ops = "login";
			login.roomCode = roomCode;
			login.userData = JSON.encode(UserVO.self.convertToObject());
			login.t = Math.random();
			var request:URLRequest = new URLRequest(flashVars.keepPage);
			request.data = login;
			request.method = URLRequestMethod.POST;
			load(request);
		}
		[DexterExternal]
		public function sendRoomPassword(pwd:String):void{
			login.t  = Math.random();
			login.pwd = pwd;
			var request:URLRequest = new URLRequest(flashVars.keepPage);
			request.data = login;
			request.method = URLRequestMethod.POST;
			load(request);
		}
		[DexterEvent][DexterExternal]
		public function changeUserPropertyFromJs(id:String,name:String,value:*):void{
			var variables:URLVariables = new URLVariables();
			variables.ops = "changeUserP";
			variables.id = id;
			variables.name = name;
			variables.value = value;
			variables.t = Math.random();
			var request:URLRequest = new URLRequest(flashVars.keepPage);
			request.data = variables;
			request.method = URLRequestMethod.POST;
			load(request);
			sendDexterEvent("broadcast","changeUserProperty",id,name,value);
		}
		[DexterEvent][DexterExternal]
		public function changeAppPropertyFromJs(name:String,value:*):void{
			var variables:URLVariables = new URLVariables();
			variables.ops = "changeAppP";
			variables.name = name;
			switch(name){
				case "同步文档":
					variables.value = JSON.encode(value);
					break;
				default:
					variables.value = value;
					break;
			}
			variables.t = Math.random();
			var request:URLRequest = new URLRequest(flashVars.keepPage);
			request.data = variables;
			request.method = URLRequestMethod.POST;
			load(request);
			sendDexterEvent("broadcast","changeAppProperty",name,value);
		}
		[DexterExternal]
		public function getDeviceConfig(userId:String):void{
			var user:UserVO = sendDexterEvent("getUserByID",userId);
			if(user&&user.isSelf)sendDexterEvent("callJs","setDeviceConfigDialog",sendDexterEvent("$getDeviceConfig"));
			else if(user) sendDexterEvent("sendToUser",user,"getDeviceConfig",UserVO.self.groupAddress);
		}
		[DexterExternal]
		public function windowClose():void{
			load(new URLRequest(flashVars.keepPage+"?ops=logoff&t="+Math.random()));
			sendDexterEvent("broadcast","userOffline",UserVO.self.id);
			stratus.close();
			keepTimer.stop();
		}
		private var requestList:Vector.<URLRequest> = new Vector.<URLRequest>();
		private var isLoading:Boolean;
		override public function load(request:URLRequest):void{
			if(isLoading){
				requestList.push(request);
			}else{
				super.load(request);
				isLoading = true;
			}
		}
	}
}